/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Controllers.utils.HibernateUtil;
import Controllers.utils.URLUtils;
import Controllers.validation.EmailValidator;
import Controllers.validation.PasswordValidator;
import Model.Client;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;

/**
 *
 * @author Anthares
 */
public class Register extends BaseServlet {
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request  servlet request
     * @param response servlet response
     * <p/>
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HashMap<String, String> requestedRessource = URLUtils.getRequestedRessource(getServletContext(), request);
        String action = requestedRessource.get("action");
        log(action);
        if(action == null){
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        } else {
            if(action.equals("confirm")){
                confirmAction(request, response);
            } else {
                request.setAttribute("errorCode", 404);
                request.getRequestDispatcher("/error.jsp").forward(request, response);
            }
        }
    }

    private void confirmAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try{
            String name = request.getParameter("nom").trim();
            String firstName = request.getParameter("prenom").trim();
            String eMail = request.getParameter("email").trim();
            String password = request.getParameter("password").trim();
            if(paramsAreValids(name, firstName, eMail, password)){
                Client user = new Client(name, firstName, eMail, password);
                Session sess = HibernateUtil.getSessionFactory().openSession();
                sess.getTransaction().begin();
                sess.save(user);
                sess.getTransaction().commit();
                sess.close();
                request.getSession(true).setAttribute("user", user);
                request.getRequestDispatcher("/home").forward(request, response);
            } else {
                request.setAttribute("invalidFields", true);
                request.getRequestDispatcher("/register.jsp").forward(request, response);
            }
        } catch(NullPointerException ex){
            request.setAttribute("emptyFields", true);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }
    }

    private boolean paramsAreValids(String name, String firstName, String eMail, String password) {
        if(name.equals("") || name.length() < 2){
            return false;
        }
        if(firstName.equals("") || name.length() < 2){
            return false;
        }
        if(eMail.equals("") || !new EmailValidator().isValid(eMail)){
            return false;
        }
        if(password.equals("") || !new PasswordValidator().isValid(password)){
            return false;
        }
        return true;
    }
}
